package com.msb.dongbao.portal.web.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author: mayu
 * @date: 2021年05月31日 11:41
 * 获取请求中的url和body参数
 */
@Slf4j
public class HttpParamUtils {

    /**
     * 获取请求中的所有参数
     *
     * @param request
     * @return java.util.SortedMap
     */
    public static SortedMap getAllParams(HttpServletRequest request) {
        SortedMap<String, String> appParams = new TreeMap<>();
        Map<String, String> urlParams = getUrlParams(request);
        Map<String, String> bodyParams = getBodyParams(request);
        if (MapUtil.isNotEmpty(urlParams)) {
            for (Map.Entry<String, String> entry : urlParams.entrySet()) {
                appParams.put(entry.getKey(), entry.getValue());
            }
        }
        if (MapUtil.isNotEmpty(bodyParams)) {
            for (Map.Entry<String, String> entry : bodyParams.entrySet()) {
                appParams.put(entry.getKey(), entry.getValue());
            }
        }
        return appParams;
    }

    /**
     * 获取url中的参数
     *
     * @param request
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    private static Map<String, String> getUrlParams(HttpServletRequest request) {
        HashMap<String, String> map = MapUtil.newHashMap();

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            String parameterValue = request.getParameter(parameterName);
            map.put(parameterName, parameterValue);
        }

        /*if (StringUtils.isNotBlank(request.getQueryString())) {
            try {
                String queryString = URLDecoder.decode(request.getQueryString(), "UTF-8");
                String[] split = queryString.split("&");
                for (String s : split) {
                    int i = s.indexOf("=");
                    String key = s.substring(0, i);
                    String value = s.substring(i + 1);
                    map.put(key, value);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }*/

        System.out.println("url参数：" + map);
        return map;
    }

    /**
     * 获取body中的参数
     *
     * @param request
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    public static Map<String, String> getBodyParams(HttpServletRequest request) {
        Map<String, String> map = null;
        Map<String, String> stringStringMap = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String s = "";
            while ((s = br.readLine()) != null) {
                sb.append(s);
            }
            map = JSONObject.parseObject(sb.toString(), Map.class);
            log.info("body map参数：" + map);
            stringStringMap = Convert.toMap(String.class, String.class, sb);
            log.info("body stringStringMap参数：" + map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }
}
